package cj.web.admin.controllers;

import java.util.List;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.dao.SurveyDAO;
import cj.web.admin.dao.SurveyGroupDAO;
import cj.web.admin.dao.SurveyPeopleDAO;
import cj.web.admin.domain.Survey;
import cj.web.admin.domain.SurveyGroup;
import cj.web.admin.domain.SurveyPeople;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/surveyPeople")
public class SurveyPeopleController extends BaseController{

	private static Logger logger = Logger.getLogger(SurveyPeopleController.class.getName());
	
	@Autowired
	private SurveyDAO surveyDao;
	@Autowired
	private SurveyPeopleDAO surveyPeopleDao;
	@Autowired
	private SurveyGroupDAO surveyGroupDAO;
	
	@Get("show")
	public String show(Model model,SurveyPeople surveyPeople) {
	
		List<SurveyGroup> surveylis = surveyGroupDAO.queryAll();
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgksryxx);
        model.add("surveylis",surveylis);
		return "/views/survey/surveyPeopleList.vm";
	}

	@Post("getList")
	public String queryList(SurveyPeople surveyPeople) {
		JSONObject jo = new JSONObject();
		List<SurveyPeople> arr = surveyPeopleDao.query(surveyPeople);
		jo.put("data", arr);
		jo.put("draw", surveyPeople.getDraw());
		return "@" + jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id){
		SurveyPeople surveyPeople = surveyPeopleDao.queryById(id);
        //MemuEnum
        model.add("MemuEnum",MemuEnum.lkgksryxx);
        model.add("obj",surveyPeople);
		return "/views/survey/surveyPeopleEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,@Param("id") int id,@Param("score") int score){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			surveyPeopleDao.update(id, score);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id){
		surveyPeopleDao.deleteById(id);
		return "r:show";
	}
}